package com.info.pdf.work;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import swings.mycomponent.panel.JStatusBarPanel;

import com.info.pdf.base.EncryptUtils;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * @Title: SwingWorkerWinApp.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2011-3-8
 * @version V1.0
 */

public class SwingWorkerWinApp {

	private static Logger log = Logger.getLogger(SwingWorkerWinApp.class);

	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("com.info.pdf.messages"); //$NON-NLS-1$

	private JFrame frmPdf;
	private final JPanel footpanel = new JStatusBarPanel(" 首都标准网.专用 ",
			"文献整理工具   ");

	private final SwingWorkerJPanel panel = new SwingWorkerJPanel();

	private static Desktop desktop;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		} catch (Throwable e) {

			e.printStackTrace();

		}

		if (Desktop.isDesktopSupported()) {
			desktop = Desktop.getDesktop();
		} else {
			System.out.println("Desktop class is not supported");
			System.exit(1);
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SwingWorkerWinApp window = new SwingWorkerWinApp();
					window.frmPdf.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public SwingWorkerWinApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		// 基本设置
		frmPdf = new JFrame();
		frmPdf.setResizable(false);
		frmPdf.setIconImage(Toolkit.getDefaultToolkit()
				.getImage(
						SwingWorkerWinApp.class
								.getResource("/com/info/source/pdf.png")));

		frmPdf.setTitle(BUNDLE.getString("SwingWorkerWinAppS.frmPdf.title")); //$NON-NLS-1$
		frmPdf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 窗口居中
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getCenterPoint();
		int windowWidth = 450;
		int windowHeight = 300;

		frmPdf.setBounds(center.x - windowWidth / 2, center.y - windowHeight
				/ 2, windowWidth, windowHeight);

		// 引入定制好的 Jpanel

		final JBackgroundImagePanel backgroundPanel = new JBackgroundImagePanel();
		backgroundPanel.setBackground(new ImageIcon(JBackgroundImagePanel.class
				.getClass().getResource("/com/info/source/back.png")));
		frmPdf.getContentPane().add(backgroundPanel, BorderLayout.CENTER);
		backgroundPanel.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("40px"), FormFactory.PREF_COLSPEC, },
				new RowSpec[] { RowSpec.decode("208px"),
						RowSpec.decode("24px"), }));

		frmPdf.getContentPane().add(footpanel, BorderLayout.SOUTH);

		JButton button = new JButton("开始解密");

		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				System.out.println("hello");

				backgroundPanel.setVisible(false);
				frmPdf.getContentPane().add(panel, BorderLayout.CENTER);

				// 引入定制好的 Jpanel
				frmPdf.remove(backgroundPanel);

				// 验证版权
				// File encryptFile = new File("c:\\sncode.txt");
				// String decryptpassword;
				// String encryptpassword;
				//
				// try {
				// List<String> list = FileUtils.readLines(encryptFile);
				//
				// if (list == null || list.size() == 0)
				// return;
				//
				// for (String s : list) {
				//
				// decryptpassword = EncryptUtils.decrypt(s);
				//
				// log.info("decryptpassword :" + decryptpassword);
				// int temp = Integer.valueOf(decryptpassword);
				//
				// log.info("temp :" + temp);
				//
				// if (temp <= 20) {
				//
				// String temps = String.valueOf(temp - 1);
				// log.info("temps :" + temps);
				//
				// encryptpassword = EncryptUtils.encrypt(temps);
				//
				// log.info("encryptpassword :" + encryptpassword);
				//
				// FileUtils.writeStringToFile(encryptFile,
				// encryptpassword);
				//
				// backgroundPanel.setVisible(false);
				// frmPdf.getContentPane().add(panel,
				// BorderLayout.CENTER);
				//
				// break;
				// }
				//
				// else {
				// return;
				// }
				//
				// }
				//
				// } catch (Exception e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }

				System.out.println("hello2");

			}
		});

		backgroundPanel.add(button, "2, 2, left, bottom");
		// frame.getContentPane().add(footpanel, BorderLayout.SOUTH);

	}
}
